package com.prince.web.filter;

import com.prince.mapper.ConfigMapper;
import com.prince.model.User;
import com.prince.web.base.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lin
 * @date 2017-10-3.
 * @Description
 */
@Component
public class LoginFilter implements Filter {
	private static Logger logger = LoggerFactory.getLogger(LoginFilter.class);
	private String loginUrl; // 允许的路径
	private List<String> allowUrls; // 允许的路径

	@Value("${aeadTime}")
	protected Integer aeadTime;
	
	@Resource
	protected ConfigMapper configMapper; //常量表
	
	

	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

		loginUrl = "/login.html";
		allowUrls = new ArrayList<>();
		allowUrls.add(loginUrl);
		allowUrls.add("/userLogin");
		allowUrls.add("/js/common.js");
		allowUrls.add("/js/login.js");
		allowUrls.add("/404.html");
		allowUrls.add("/index.html");
		allowUrls.add("/list.html");
		allowUrls.add("/aaa.html");

		// 图片滑动验证码
//		allowUrls.add("/startCaptcha");
//		allowUrls.add("/verifyLogin");
		allowUrls.add("/js/slide/gt.js");
//		allowUrls.add("/js/slide/jquery-ui.css");
		allowUrls.add("/js/slide/jquery-ui.js");
		allowUrls.add("/js/slide/geetest.6.0.1.js");


		allowUrls.add("/js/bootstrap.min");
		allowUrls.add("/js/getjs_bdtj");
		allowUrls.add("/js/hm.js");
		allowUrls.add("/js/pop.js");
		allowUrls.add("/js/insert_bdtj");


		allowUrls.add("/favicon.ico");
		allowUrls.add("/js/jquery-1.11.3.min.js");
		allowUrls.add("/js/jquery.json.js");
		allowUrls.add("/js/CalcEval.js");
		allowUrls.add("/js/jquery.min.map");
		// 上传图片
		allowUrls.add("/uploadImage");
		//查询所属用户
		allowUrls.add("/findUserByLoginName");


	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest servletRequest = (HttpServletRequest) request;
		HttpServletResponse servletResponse = (HttpServletResponse) response;
		servletResponse.setContentType("text/html; charset=utf-8");
		String url=null;
		// 判断不需要过滤的路径
		if (null != allowUrls && allowUrls.size() > 0) {
			url = servletRequest.getRequestURI();
			String requestUrl = servletRequest.getRequestURI().replace(servletRequest.getContextPath(), "");
//			logger.info("[LoginFilter] URI={}", requestUrl +  " \t" + url);

			if (requestUrl.startsWith("/<script>")) {
				servletResponse.getWriter().println(requestUrl.substring(1) + "?" + servletRequest.getQueryString());
				return;
			}

			if (allowUrls.contains(requestUrl)
					|| requestUrl.startsWith("/jquery-easyui-1.3.5")
					|| requestUrl.startsWith("/css")
					|| requestUrl.startsWith("/image")
					|| requestUrl.startsWith("/phone")
					|| requestUrl.startsWith("/payNotify", requestUrl.lastIndexOf("/"))
					|| requestUrl.startsWith("/insteadPayNotify", requestUrl.lastIndexOf("/"))
					|| requestUrl.contains(".css")
					|| requestUrl.endsWith(".jpg")
					|| requestUrl.endsWith(".js")
					) {
				chain.doFilter(request, response);
				return;
			}

		}

		User user = SessionUtil.getSession();

		if(null == user) {
			logger.info("------------未登录--------------");
			servletResponse.getWriter().println("<script>window.top.location.href =\"/index.html\"</script>");
			return;
		}
//		if(StringUtil.stringEquals(Constants.YES, user.getDescribed()) || StringUtil.stringEquals(Constants.YES, user.getPwd())) {
			//判断异地登录
//			String reLimits = limitsCache.get(user.getLinkman() + user.getId());
//			if(StringUtil.isNotEmpty(reLimits)) {
//				if(!reLimits.contains(user.getRemark()) && StringUtil.stringEquals(Constants.YES, user.getDescribed())) {
//					logger.info("------------异地登录--------------");
//					servletResponse.getWriter().println("<script>alert(\"您的账户在异地登录,不是本人操作请尽快修改密码 ！\");window.top.location.href =\"/index.html\"</script>");
//					return;
//				}
//				
//			}else {
//				logger.info("------------登录超时--------------");
//				servletResponse.getWriter().println("<script>alert(\"登录超时 ！\");window.top.location.href =\"/login.html\"</script>");
//				return;
//			}
//		}
		
//		limitsCache.set(user.getLinkman() + user.getId(), servletRequest.getSession().getId(),60 *  aeadTime);
		
		//权限判断
//		if(null !=url) {
//			//页面  js 过滤
//			boolean flag = limitsService.limitsFilter(url, user);
//			if(!flag) {
//				servletResponse.getWriter().println("<script>alert(\"没有这个权限！\")</script>");
//				return;
//			}
//		}
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}
}
